package com.chorye.excel.controller;

import com.chorye.excel.common.excel.ExportExcel;
import com.chorye.excel.common.excel.utils.DateUtils;
import com.chorye.excel.entity.Student;
import com.chorye.excel.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Company: 北京XXXX有限公司
 * @Author JiangYy
 * @Title: ExportController
 * @Description: TODO
 * @Date 2019/1/15 11:28
 */
@Slf4j
@RestController
public class ExportController {

    @Autowired
    private StudentService service;

    /**
     * 导出用户数据
     *
     * @param response
     * @return
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public void exportFile(HttpServletResponse response) {
        try {
            String fileName = "学生数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            new ExportExcel("学生数据", Student.class).setDataList(service.list(null)).write(response, fileName).dispose();
        } catch (Exception e) {
            log.error("导出出错: ", e.getMessage());
        }
    }
}
